// components/list-item/list-item.js
const getDistance = require("../../utils/getCity.js")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    jobList: Array
  },
  data: {
    show: true,
    showLoading: "正在努力的加载...",
    circle: "circle",
    benifit: [],
    timebox: [],
    distance: []
  },
  observers: {
    "jobList": function () {
      const that = this;
      var box = this.data.jobList;
      for (let i in box) {
        let benifit = box[i].benifit.split(",");
        this.data.benifit.push(benifit)
        this.data.timebox.push(this.getTime(box[i].createdTime))
        getDistance.getDistance([{
          latitude: box[i].latitude,//39.915405, 116.403802
          longitude: box[i].longitude
        }]).then(res => {
          if (res < 1000) {
            that.data.distance.push(res + "m");
          } else {
            that.data.distance.push((res / 1000).toFixed(1) + "km");
          }
          that.setData({
            distance: that.data.distance,
          })
        })
      }
      that.setData({
        benifit: that.data.benifit,
        timebox: that.data.timebox,
        distance: that.data.distance,
      })
    }
  },
  ready() {
    const that = this;
    setTimeout(function () {
      that.setData({
        show: false,
      })
    }, 3000)
  },
  methods: {
    tomore: function (e) {
      console.log(e.currentTarget.dataset)
      var url = '/pages/recruit/details/details?partTimeId=' + e.currentTarget.dataset.parttimeid;
      wx.navigateTo({
        url: url,
      })
    },
    getTime: function (time) {
      var date1 = new Date(); //新建一个日期对象，默认现在的时间
      var date2 = new Date(time); //设置过去的一个时间点，"yyyy-MM-dd HH:mm:ss"格式化日期
      var date3 = date1 - date2;
      //计算出相差天数
      var days = Math.floor(date3 / (24 * 3600 * 1000))
      //计算出小时数
      var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
      var hours = Math.floor(leave1 / (3600 * 1000))
      //计算相差分钟数
      var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
      var minutes = Math.floor(leave2 / (60 * 1000))
      //计算相差秒数
      var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
      var seconds = Math.round(leave3 / 1000)
      // console.log(days + "----" + hours + "----" + minutes + "----" + seconds)
      if(days > 0){
        return days+"天前";
      }else if(hours > 0){
        return hours + "小时前";
      }else if(minutes > 0){
        return minutes + "分钟前";
      }else {
        return seconds + "秒前";
      }
    }
  }
})